package com.ai.sml.interceptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.filter.mgt.FilterChainManager;
import org.apache.shiro.web.filter.mgt.FilterChainResolver;
import org.apache.shiro.web.filter.mgt.PathMatchingFilterChainResolver;
import org.apache.shiro.web.mgt.WebSecurityManager;
import org.apache.shiro.web.servlet.AbstractShiroFilter;
import org.springframework.beans.factory.BeanInitializationException;


/**
 * 初始化权限列表及权限级别
 *
 */
public class InitFilterFactoryBean extends ShiroFilterFactoryBean{
	private final static String USER_CD = "1";
	private final static String AUTHC_CD = "2";
	private final static String ANON_CD = "3";
	
	private final static String USER = "user";
	private final static String AUTHC = "authc";
	private final static String ANON = "anon";
	
	private final static String URL_PERMS = "urlPerms";
	private final static String PERMS = "perms";
	
//	private final static String REMEMBER_FILETER = "rememberFilter";
	


    protected AbstractShiroFilter createInstance() throws Exception {

        SecurityManager securityManager = getSecurityManager();
        if (securityManager == null) {
            String msg = "SecurityManager property must be set.";
            throw new BeanInitializationException(msg);
        }
        if (!(securityManager instanceof WebSecurityManager)) {
            String msg = "The security manager does not implement the WebSecurityManager interface.";
            throw new BeanInitializationException(msg);
        }

        FilterChainManager manager = createFilterChainManager();
        ArrayList<String> permsList = new ArrayList<String>();
        permsList.add("selfcenter.html");
//        for(Object perms:permsList){
//        	Map map = (Map)perms;
//        	//读取菜单的URL
//        	String tempUrl=(String)map.get("URL_ADDR");
//        	if(StringUtil.isEmpty(tempUrl)){
//        		continue;
//        	}
//        	//过滤菜单的URL
//        	String urlAddr =StringUtil.replace(tempUrl);
//        	
//        	String acctAccessLmtCd = (String)map.get("ACCT_ACCESS_LMT_CD");
//        	
//        	//
//        	if(acctAccessLmtCd.equals(USER_CD)){
//        		manager.addToChain(urlAddr, USER);
//        	}else if(acctAccessLmtCd.equals(AUTHC_CD)){
//        		manager.addToChain(urlAddr, AUTHC);
//        	}else if(acctAccessLmtCd.equals(ANON_CD)){
//        		manager.addToChain(urlAddr, ANON);
//        	}
//        	//
//        	String uiPrivCd = (String)map.get("UI_PRIV_CD");
//        	String tsvldUrlParaFlag = (String)map.get("TSVLD_URL_PARA_FLAG");
//        	if(tsvldUrlParaFlag.equals("Y")){
//        		manager.addToChain(urlAddr, URL_PERMS,urlAddr);
//        	}else{
//        		manager.addToChain(urlAddr, PERMS,uiPrivCd);
//        	}
//
//        }
        
        manager.addToChain("selfcenter.html", AUTHC);
        manager.addToChain("selfcenter.html", URL_PERMS, "selfcenter.html");
        
        //权限列表中的信息，必须已经登录，所以全部设置为authc
        //除权限列表 以及 配置文件里设置的之后，其它所有数据默认必须登录(此项必须放到最后，因为过滤根据顺序执行，
        //小范围 放在前边，大范围放在后边。若大范围 放在前边，小范围就会被覆盖)
//        manager.addToChain("/**", REMEMBER_FILETER);
        PathMatchingFilterChainResolver chainResolver = new PathMatchingFilterChainResolver();
        chainResolver.setFilterChainManager(manager);

        return new SpringShiroFilter((WebSecurityManager) securityManager, chainResolver);
    }
    private static final class SpringShiroFilter extends AbstractShiroFilter {

        protected SpringShiroFilter(WebSecurityManager webSecurityManager, FilterChainResolver resolver) {
            super();
            if (webSecurityManager == null) {
                throw new IllegalArgumentException("WebSecurityManager property cannot be null.");
            }
            setSecurityManager(webSecurityManager);
            if (resolver != null) {
                setFilterChainResolver(resolver);
            }
        }
    }
}
